package com.sun.BmobDemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegActivity extends ActionBarActivity implements View.OnClickListener{

    private String phoneNum;
    private Boolean SmsCheckFlag = true;
    private TextView exit;
    private EditText edittextPhone;
    private Button buttonSendSms;
    private EditText edit_checkNum;
    private EditText edit_UserName;
    private EditText edit_Pwd;
    private EditText edit_conPwd;
    private Button buttonReg;
    private CheckBox checkBoxReg;
    private TextView text_terms;
    private Bmob0 bmob;

    private void assignViews() {
        exit = (TextView) findViewById(R.id.textView_exit);
        edittextPhone = (EditText) findViewById(R.id.edittext_phone);
        buttonSendSms = (Button) findViewById(R.id.button_sendSms);
        edit_checkNum = (EditText) findViewById(R.id.editText_checkNum);
        edit_UserName = (EditText) findViewById(R.id.editText_UserName);
        edit_Pwd = (EditText) findViewById(R.id.editText_Pwd);
        edit_conPwd = (EditText) findViewById(R.id.editText_confirmingPwd);
        buttonReg = (Button) findViewById(R.id.button_reg);
        checkBoxReg = (CheckBox) findViewById(R.id.checkBox_reg);
        text_terms = (TextView) findViewById(R.id.textView_terms);

        exit.setOnClickListener(this);
        buttonSendSms.setOnClickListener(this);
        buttonReg.setOnClickListener(this);
        text_terms.setOnClickListener(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_activity);
        assignViews();
        bmob = new Bmob0(this,"784b5674076d5c684fe1a76f2042409c");

    }

    @Override
    public void onClick(View v) {
        phoneNum = edittextPhone.getText().toString();
        switch (v.getId()){
            case R.id.textView_exit:
                Intent intentToLogin =new Intent();
                finish();
                break;
            case R.id.button_sendSms:
                if(phoneNum.equals("")) {
                    Toast.makeText(this, "手机号不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }else if(phoneNum.length() < 11) {
                    Toast.makeText(this, "手机号位数不对！", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    bmob.sendSMSCheckedNum("13624267104","sms");
                }
                break;
            case R.id.button_reg:
                String checkNum = edit_checkNum.getText().toString();
                String UserName = edit_UserName.getText().toString();
                String Pwd = edit_Pwd.getText().toString();
                String conPwd = edit_conPwd.getText().toString();
//                if(checkNum.equals("")){
//                    Toast.makeText(this, "验证码不能为空！", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                //if(!SmsCheckFlag){
//                    Boolean flag = bmob.checkSmsNum(phoneNum, checkNum);
//                    if(flag){
//                        Toast.makeText(RegActivity.this, flag.toString(), Toast.LENGTH_SHORT).show();
                   // }
//                    BmobSMS.verifySmsCode(this, phoneNum, checkNum, new VerifySMSCodeListener() {
//                        @Override
//                        public void done(BmobException ex) {
//                            if (ex == null) {//短信验证码已验证成功
//                                SmsCheckFlag = true;
//                                Toast.makeText(RegActivity.this, "验证码不能为空！"+ex.toString(), Toast.LENGTH_SHORT).show();
//                                Log.i("bmob", "验证通过");
//                            } else {
//                                Toast.makeText(RegActivity.this, "验证码不正确！", Toast.LENGTH_SHORT).show();
//                                Log.i("bmob", "验证失败：code =" + ex.getErrorCode() + ",msg = " + ex.getLocalizedMessage());
//                                return;
//                            }
//                        }
//                    });
//                }
                if(UserName.equals("")){
                        Toast.makeText(this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
                        return;
                }

//                if(Pwd.equals("")){
//                        Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
//                        return;
//                }
//                if(conPwd.equals("")){
//                        Toast.makeText(this, "确认密码不能为空！", Toast.LENGTH_SHORT).show();
//                        return;
//                }
//                if(!Pwd.equals(conPwd)){
//                    Toast.makeText(this, "两次密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                if(!checkBoxReg.isChecked()){
//                    Toast.makeText(this, "请阅读用户协议并选中！", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                //bmob.queryAll();
                //bmob.del(UserName);
               // Log.i("1","0");
               // bmob.queryAll();
              // List<BmobObject> one = bmob.queryBeans;
               // Toast.makeText(this, "vhengg"+one.toString(), Toast.LENGTH_SHORT).show();
                //Log.i("1","7");
//                AlertDialog.Builder builder = new AlertDialog.Builder(this);
//                builder.setTitle("query");
//                String str = "";
//                for(UserAccount user:one){
//                    str+=user.getAname()+""+user.getPhoneNum()+"\n";
//                }
//                builder.setMessage(str);
//                builder.create().show();
                //Toast.makeText(this, one.toString(), Toast.LENGTH_SHORT).show();
                //Log.i("1", "9");
//                AlertDialog.Builder builder = new AlertDialog.Builder(this);
//                builder.setTitle("query");
//                String str = "";
//                for(UserAccount user:one){
//                    str+=user.getAname()+""+user.getPhoneNum()+"\n";
//                }
//                builder.setMessage(str);
//                builder.create().show();
//                        queryOneBmobData(UserName, "name");
//                Toast.makeText(this, one.size(), Toast.LENGTH_SHORT).show();
//                SystemClock.sleep(2000);
//                Toast.makeText(this, one.toString(), Toast.LENGTH_SHORT).show();
//
//                UserAccount userInfo;
//                if(one != null){
//                    userInfo = one.get(0);
//                    userInfo.setPhoneNum("13624267104");
//                    userInfo.setPwd("123");
//                    bmob.updata(userInfo.getObjectId(), userInfo);
//                }




//                List<UserAccount> u = bmob.queryAll();
//                if(u != null) {
//                    Toast.makeText(this, "用户已经存在"+u.toString(), Toast.LENGTH_SHORT).show();
//                }else{
//                    Toast.makeText(this, "用户不存在", Toast.LENGTH_SHORT).show();
//                }

                UserAccount userAccount = new UserAccount();
                userAccount.setName(UserName);
                userAccount.setPwd(Pwd);
                userAccount.setPhoneNum(phoneNum);
                //bmob.updata(userAccount);
//                if(u == null){

                    bmob.regUserAccount(userAccount, null, "LoginActivity.class");
                    finish();
                Log.i("1","reg");


//                Toast.makeText(this, "注册成功！"+phoneNum+""+UserName+""+Pwd+""+conPwd+""+checkBoxReg, Toast.LENGTH_SHORT).show();
//                }

                SmsCheckFlag = false;
                Log.i("1",phoneNum+" "+UserName+" "+Pwd+" "+conPwd+" "+checkBoxReg);

                break;
            case R.id.textView_terms:
                Intent intent = new Intent(this,terms.class);
                startActivity(intent);
                break;

        }

    }
}
